package com.cloudweb.oa.service.impl;

import cn.js.fan.db.ResultIterator;
import cn.js.fan.db.ResultRecord;
import cn.js.fan.util.ErrMsgException;
import cn.js.fan.util.StrUtil;
import com.cloudweb.oa.service.ProjectService;
import com.cloudwebsoft.framework.db.JdbcTemplate;
import com.redmoon.oa.flow.FormDb;
import com.redmoon.oa.flow.FormDAO;
import com.redmoon.oa.flow.WorkflowDb;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.Iterator;

@Service
public class ProjectServiceImpl implements ProjectService {

    /**
     * 分包合同结算报帐结束事件
     *
     * @throws ErrMsgException
     */
    @Override
    public void onFbjsBzFinish(HttpServletRequest request, WorkflowDb wf, FormDAO fdao) throws ErrMsgException {
        long id = fdao.getId();
        // 分包合同ID
        String htId = fdao.getFieldValue("source_gcht");

        // 分包合同结算明细 contr_sub_sett_dtl
        FormDb fdJsNest = new FormDb();
        fdJsNest = fdJsNest.getFormDb("contr_sub_sett_dtl");
        FormDb fdPlanNest = new FormDb();
        fdPlanNest = fdPlanNest.getFormDb("contr_sub_playplan");
        FormDb fdBudgetDtl = new FormDb();
        fdBudgetDtl = fdBudgetDtl.getFormDb("prj_budget_dtl");

        // 遍历分包合同报帐明细表
        String sql = "select id from " + FormDb.getTableName("prj_fbht_bz_nest") + " where cws_id = " + StrUtil.sqlstr(String.valueOf(id));
        com.redmoon.oa.visual.FormDAO dataDao = new com.redmoon.oa.visual.FormDAO();
        Iterator it = dataDao.list("prj_fbht_bz_nest", sql).iterator();
        while (it.hasNext()) {
            dataDao = (com.redmoon.oa.visual.FormDAO) it.next();

            double bczfje = StrUtil.toDouble(dataDao.getFieldValue("bczfje"), 0);

            long quoteId = dataDao.getCwsQuoteId();

            // 根据quoteId取结算明细
            com.redmoon.oa.visual.FormDAO jsDao = new com.redmoon.oa.visual.FormDAO();
            jsDao = jsDao.getFormDAO(quoteId, fdJsNest);
            if (!jsDao.isLoaded()) {
                throw new ErrMsgException("分包合同结算明细表中对应项不存在");
            }

            // 置结算明细中的已付、未付
            double bcjsje = StrUtil.toDouble(jsDao.getFieldValue("bcjsje"), 0);
            double yfje = StrUtil.toDouble(jsDao.getFieldValue("yfje"), 0);
            yfje += bczfje;
            double wfje = bcjsje - yfje;
            jsDao.setFieldValue("yfje", String.valueOf(yfje));
            jsDao.setFieldValue("wfje", String.valueOf(wfje));
            jsDao.save();

            // 取分包合同款项计划
            com.redmoon.oa.visual.FormDAO planDao = new com.redmoon.oa.visual.FormDAO();
            planDao = planDao.getFormDAO(jsDao.getCwsQuoteId(), fdPlanNest);
            // 置分包合同款项计划中的已付、未付
            double yjje = StrUtil.toDouble(planDao.getFieldValue("yjje"), 0);
            yfje = StrUtil.toDouble(planDao.getFieldValue("yfje"), 0);
            yfje += bczfje;
            wfje = yjje - yfje;
            planDao.setFieldValue("yfje", String.valueOf(yfje));
            planDao.setFieldValue("wfje", String.valueOf(wfje));
            planDao.save();

            long fbhtId = StrUtil.toLong(planDao.getCwsId(), 0);

            long budgetId = -1;

            // 取分包合同中的预算明细
            sql = "select id from " + FormDb.getTableName("contr_sub_ysmx") + " where cws_id = " + StrUtil.sqlstr(htId);
            dataDao = new com.redmoon.oa.visual.FormDAO();
            it = dataDao.list("contr_sub_ysmx", sql).iterator();
            while (it.hasNext()) {
                dataDao = (com.redmoon.oa.visual.FormDAO) it.next();

                quoteId = dataDao.getCwsQuoteId();

                com.redmoon.oa.visual.FormDAO yDao = new com.redmoon.oa.visual.FormDAO();
                // 取对应的项目预算费用明细
                yDao = yDao.getFormDAO(quoteId, fdBudgetDtl);
                yjje = StrUtil.toDouble(yDao.getFieldValue("yjje"), 0);
                yfje = StrUtil.toDouble(yDao.getFieldValue("yfje"), 0);
                yfje += bczfje;
                wfje = yjje - yfje;
                yDao.setFieldValue("yfje", String.valueOf(yfje));
                yDao.setFieldValue("wfje", String.valueOf(wfje));
                yDao.save();

                budgetId = StrUtil.toLong(yDao.getCwsId(), -1);
            }

            // 项目总预算
            // 汇总阶段表，统计出总的已结金额，回写至主表单中
            FormDb fd = new FormDb();
            fd = fd.getFormDb("prj_budget");

            com.redmoon.oa.visual.FormDAO ysFdao = new com.redmoon.oa.visual.FormDAO();

            ysFdao = ysFdao.getFormDAO(budgetId, fd);

            // 统计出预算主表中的已结总额
            double yjjeSum = 0;
            sql = "select sum(yjje) from " + FormDb.getTableName("prj_budget_node") + " where cws_id=" + budgetId;
            JdbcTemplate jt = new JdbcTemplate();
            ResultIterator ri = null;
            try {
                ri = jt.executeQuery(sql);
                if (ri.hasNext()) {
                    ResultRecord rr = (ResultRecord) ri.next();
                    yjjeSum = rr.getDouble(1);
                }
            } catch (SQLException e) {
                e.printStackTrace();
                throw new ErrMsgException(e.getMessage());
            }

            ysFdao.setFieldValue("yjje", String.valueOf(yjjeSum));

            // 取出预算总额，以计算可用预算
            double ysze = StrUtil.toDouble(ysFdao.getFieldValue("ysze"), 0);
            ysFdao.setFieldValue("yjje", String.valueOf(yjjeSum));
            double kyys = ysze - yjjeSum;
            ysFdao.setFieldValue("kyys", String.valueOf(kyys));
            ysFdao.save();
        }
    }
}
